var BB = {};

//调整页面缩放比例
$(function() {
    var viewport = document.querySelector("meta[name=viewport]");
    var winWidths = $(window).width();
    var densityDpi = 640 / winWidths;
    densityDpi = densityDpi > 1 ? 300 * 640 * densityDpi / 640 : densityDpi;
    if (isWeixin()) {
        viewport.setAttribute('content', 'width=640, target-densityDpi=' + densityDpi + ', maximum-scale=' + 0.5 + ', user-scalable=no');
    } else {
        viewport.setAttribute('content', 'width=640, user-scalable=no');
        setTimeout(function() {
            viewport.setAttribute('content', 'width=640, user-scalable=no');
        }, 1000);
    }
    function isWeixin() {
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger") {
            return true;
        } else {
            return false;
        }
    }

//调整页面结束

    BB.windheight = $('.fullscreen_box').height();
    BB.adjustIndexbg = function () {
        var img = $('#indexbg'),
            img_k = img.height() / img.width(),
            wind_k = BB.windheight / 640;
        if (img_k > wind_k) {
            img.width(640);
            img.height(640 * img_k);
            img.css('top', BB.windheight / 2 - 640 * img_k / 2);
        } else {
            img.height(BB.windheight);
            img.width(BB.windheight / img_k);
            img.css('left', 320 - BB.windheight / (img_k * 2));
        }
    };
    BB.adjustIndexbg();
    setTimeout(function () {
        BB.adjustIndexbg();
    }, 500);

//页面banner图片自动轮播
    BB.moveBanner = function () {
        var box = $('#shu_banner_e'),
            ban_ul = box.find('.banner_ul'),
            ban_li = ban_ul.find('li'),
            ind_li = box.find('.ind_ul').find('li'),
            cur = 0,
            li_num = ban_li.length,
            li_wid = ban_li.width();
        ban_ul.width(li_wid * li_num);
        return function (ind) {
            if (arguments.length === 0) {
                cur = cur + 1;
                if (cur > li_num - 1) {
                    cur = 0;
                }
                ;
            } else if (arguments.length === 1) {
                if (ind < 0) {
                    cur = cur - 1;
                    if (cur < 0) {
                        cur = 0;
                    }
                    ;
                } else if (ind > 0) {
                    cur = cur + 1;
                    if (cur > li_num - 1) {
                        cur = li_num - 1;
                    }
                    ;
                }
                ;
            }
            ban_ul.animate({'left': -cur * li_wid}, 500)
            ind_li.removeClass('current').eq(cur).addClass('current');
        }
    }();
    BB.bannerId = setInterval(BB.moveBanner, 2500);
    $('#shu_banner_e').on('touchstart', function(event) {
        event.preventDefault();
        var mousedownx = event.originalEvent.targetTouches[0].pageX,
            that = $(this),
            upLeft = 0,
            movedx = 0,
            mdownTime = +new Date(),
            banner_ul = that.find('.banner_ul'),
            thisLeft = parseInt(banner_ul.css('left'), 10);
        clearInterval(BB.bannerId);
        $(window).on('touchmove', function(event) {
            var mousemovex = event.originalEvent.targetTouches[0].pageX;
            movedx = mousemovex - mousedownx;
            upLeft = thisLeft + movedx;
            banner_ul.css('left', upLeft + 'px');
        });
        $(window).on('touchend', function(event) {
            var mouseupx = event.pageX,
                upTime = +new Date(),
                upendLeft = 0;
            if (upTime > mdownTime + 300) {
                upendLeft = Math.round(Math.abs(upLeft) / 640);
                if (movedx > 320) {
                    BB.moveBanner(-1);
                } else if (movedx < -320) {
                    BB.moveBanner(1);
                } else {
                    banner_ul.animate({'left': -upendLeft * 640}, 500);
                }
            } else if (upTime < mdownTime + 300) {
                if (movedx > 50) {
                    BB.moveBanner(-1);
                } else if (movedx < -50) {
                    BB.moveBanner(1);
                } else {
                    banner_ul.animate({'left': -upendLeft * 640}, 500);
                }
            }
            ;
            BB.bannerId = setInterval(BB.moveBanner, 2500);
            $(window).off('touchmove');
            $(window).off('touchend');
        });
    });


//退出前确认
    $('#close_box_e').on('click', function(event) {
        event.preventDefault();
        var tar = $(event.target);
        var that = $(this);
        if (tar.hasClass('no')) {
            that.removeClass('now');
        }
        ;
    });

//余额取现数字判断
    $('#isnumber').on('blur', function(event) {
        var that = $(this),
            val = that.val();
        if (val) {
            $('input[type=submit]').addClass('now');
        } else {
            alert('金额输入违法！');
            that.focus();
        }
    });
//认证图片放大缩小
    $('#attestation_e').on('click', 'a', function(event) {
        var that = $(this);
        var bigimg_box = $('#bigimg_e');
        if (that.hasClass('add_btn')) {
            return;
        } else {
            bigimg_box.find('img').attr('src', that.find('img').attr('src')).end().addClass('now');
        }
    });
    $('#bigimg_e').on('click', 'a', function(event) {
        event.preventDefault();
        var that = $(this),
            box = $('#bigimg_e'),
            img = box.find('img'),
            imgw = img.width();
        if (that.hasClass('close')) {
            box.removeClass('now');
        } else if (that.hasClass('small')) {
            if (img.width() == 560) {
                return;
            } else {
                img.width(img.width() / 2);
            }
        } else if (that.hasClass('big')) {
            img.width(img.width() * 2);
        }
        ;
    });

//银行卡下拉查看信息
    $('#bank_card_e').on('click', 'a', function(event) {
        var that = $(this);
        var li = that.parents('li');
        if (that.hasClass('sliddown')) {
            if (li.hasClass('now')) {
                li.removeClass('now');
            } else {
                li.addClass('now');
            }
        }
        ;
    });

//交易明细按条件查询
    $('#search_box_e').on('click', 'a', function(event) {
        var that = $(this);
        var box = $('#search_box_e');
        if (that.hasClass('down_btn')) {
            box.addClass('now');
        } else if (that.hasClass('up_btn')) {
            box.removeClass('now');
        }
        ;
    });

//扫码收款
    $('#code_pay_e').on('click', function(event) {
        var tar = $(event.target);
        if (tar.hasClass('go_payway')) {
            $('#qq_pay_e').addClass('now');
        }
        ;
    });
    $('#qq_pay_e').on('click', function(event) {
        var tar = $(event.target);
        if (tar.hasClass('close_btn')) {
            $(this).removeClass('now');
        }
        ;
    });


});